/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.sensor.model.impl;

import nl.cloudfarming.sensor.model.GeoSensorDataFileDAO;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import nl.cloudfarming.sensor.model.GeoSensorDataFileBO;
import org.springframework.stereotype.Repository;

/**
 * @author I. de Vries
 */
@Repository
public class GeoSensorDataFileDAOImpl implements GeoSensorDataFileDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public GeoSensorDataFileBO newInstance(String fileName, String checksum, Date fileCreated, Date fileRead) {

        GeoSensorDataFileBO geoSensorDataFileBO = new GeoSensorDataFileBO(fileName, checksum, fileCreated, fileRead);

        em.persist(geoSensorDataFileBO);

        return geoSensorDataFileBO;
    }

    @Override
    public boolean mergeGeoSensorDataFile(GeoSensorDataFileBO orgGsdf){
        if (orgGsdf.getGeoSensorDataFileId() == 0) {
             em.persist(orgGsdf);
        }
        else {
            em.merge(orgGsdf);
        }
        return true;
    }

    @Override
    public List<GeoSensorDataFileBO> findAllGeoSensorDataFiles() {
        Query query = em.createQuery("select g from GeoSensorDataFileBO g");
        return query.getResultList();
    }

    @Override
    public GeoSensorDataFileBO findById(long id) {
        Query query = em.createQuery("select g from GeoSensorDataFileBO g where g.geoSensorDataFileId = :id ");
        query.setParameter("id", id);
        try {
            return (GeoSensorDataFileBO) query.getSingleResult();
        } catch (NoResultException ner){
            return null;
        }
    }

    @Override
    public GeoSensorDataFileBO findByNameChecksum(String fileName, String checksum){
        Query query = em.createQuery(
                "select g from GeoSensorDataFileBO g " +
                "where g.fileName = :fileName " +
                "and g.checksum = :checksum");
        query.setParameter("fileName", fileName);
        query.setParameter("checksum", checksum);
        try {
            return (GeoSensorDataFileBO) query.getSingleResult();
        } catch (NoResultException ner){
            return null;
        }
    }

    @Override
    public List<GeoSensorDataFileBO> findGeoSensorDataFilesByPeriod(Date dateFrom, Date dateUntil) {
        Query query = em.createQuery(
                "select g from GeoSensorDataFileBO g " +
                "where g.fileCreated >= :dateFrom " +
                "and g.fileCreated <= :dateUntil");
        query.setParameter("dateFrom", dateFrom);
        query.setParameter("dateUntil", dateUntil);
        return query.getResultList();
    }
}
